<template>
  <div class="header">
    <a href="#/">
      <img class="logo" src="@/assets/timg.jpg" width="25px" />
      <span class="title">会员管理系统</span>
    </a>
    <el-dropdown @command="handleCommand">
      <span class="el-dropdown-link">
        {{ getUser.name }}
        <i class="el-icon-arrow-down el-icon--right"></i>
      </span>
      <el-dropdown-menu slot="dropdown">
        <el-dropdown-item command="editPassword">修改密码</el-dropdown-item>
        <el-dropdown-item command="exitSys">退出系统</el-dropdown-item>
      </el-dropdown-menu>
    </el-dropdown>
    <el-dialog title="修改密码" :visible.sync="dialogFormVisible" width="500px">
      <el-form
        :model="UserInfo"
        status-icon
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="原密码" prop="oldPassword">
          <el-input
            type="password"
            v-model="UserInfo.oldPassword"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label="新密码" prop="newPassword">
          <el-input
            type="password"
            v-model="UserInfo.newPassword"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label="确认新密码" prop="checkPass">
          <el-input
            type="password"
            v-model="UserInfo.checkPass"
            autocomplete="off"
          ></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm('ruleForm')"
          >提交</el-button
        >
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import LoginUtil from "@/api/login";
export default {
  data() {
    const validateOldPass = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入密码"));
      } else {
        value = value.trim();
        if (value === "") {
          callback(new Error("请输入非空密码"));
        } else {
          callback();
        }
      }
    };
    const validateNewPass = (rule, value, callback) => {
      if (this.UserInfo.oldPassword === "") {
        callback(new Error("请先输入原密码"));
        this.UserInfo.newPassword = "";
      } else if (this.UserInfo.oldPassword === this.UserInfo.newPassword) {
        callback(new Error("新密码不能和原密码一致"));
        this.UserInfo.newPassword = "";
      }
      if (value === "") {
        callback(new Error("请输入密码"));
      } else {
        value = value.trim();
        if (value === "") {
          callback(new Error("请输入非空密码"));
        } else {
          callback();
        }
      }
    };
    const validateCheckPass = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入密码"));
      } else {
        value = value.trim();
        if (value === "") {
          callback(new Error("请输入非空密码"));
        } else {
          if (value === this.UserInfo.newPassword) {
            callback();
          } else {
            callback(new Error("新密码不一致"));
          }
        }
      }
    };
    return {
      dialogFormVisible: false,
      UserInfo: {
        oldPassword: "",
        checkPass: "",
        newPassword: ""
      },
      rules: {
        oldPassword: [{ validator: validateOldPass, trigger: "blur" }],
        newPassword: [{ validator: validateNewPass, trigger: "blur" }],
        checkPass: [{ validator: validateCheckPass, trigger: "blur" }]
      }
    };
  },
  computed: {
    getUser() {
      return JSON.parse(localStorage.getItem("mms-user"));
    }
  },
  methods: {
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    editPassword() {
      this.dialogFormVisible = true;
      this.UserInfo = {
        oldPassword: "",
        checkPass: "",
        newPassword: ""
      };
    },
    submitForm(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          const mmsUser = JSON.parse(localStorage.getItem("mms-user"));
          LoginUtil.updatePWD(
            mmsUser.id,
            this.UserInfo.oldPassword,
            this.UserInfo.newPassword
          ).then(response => {
            if (response.data.flag) {
              this.$refs[formName].resetFields();
              this.dialogFormVisible = false;
              this.$message({
                message: "密码修改成功",
                type: "success",
                duration: 1000
              });
              this.logout();
            } else {
              this.$message({
                message: response.data.message,
                type: "error",
                duration: 1000
              });
            }
          });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    logout() {
      const token = localStorage.getItem("mms-token");
      LoginUtil.logout(token).then(response => {
        const respData = response.data;
        if (respData.flag) {
          localStorage.removeItem("mms-token");
          localStorage.removeItem("mms-user");
          this.$router.push("/login");
        } else {
          this.$message({
            message: "click on item " + "修改密码",
            type: "warning",
            duration: 500
          });
        }
      });
    },
    handleCommand(command) {
      switch (command) {
        case "editPassword":
          this.editPassword();
          break;
        case "exitSys":
          this.logout();
          break;
        default:
          break;
      }
    }
  }
};
</script>
<style scoped>
.logo {
  vertical-align: middle;
  padding: 0 10px 0 40px;
}
.title {
  position: absolute;
  color: aliceblue;
}
.el-dropdown {
  float: right;
  margin-right: 40px;
}
.el-dropdown-link {
  cursor: pointer;
  color: aliceblue;
}
.el-icon-arrow-down {
  font-size: 12px;
}
</style>
